package com.timo.rabbitmq.config;

import com.timo.constant.RabbitMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


/**
 * @Description: Topic queue 和交换器的定义
 *
 * 因为要测试多个交换器的案例，为了防止队列名称重复的情况出现，这里就统一名命规范
 *  队列的结尾必是“TopicQueue”：helloTopicQueue
 *  交换器的结尾必须是“TopicExchange”：helloTopicExchange
 *  队列和交换器的绑定 （队列Bingding交换器：queueBindingExchange） 的名命格式1：helloTopicQueueBindingHelloTopicExchange
 *  队列和交换器的绑定 如果队列名称的前缀和交换器的前缀相同的情况下可以使用缩写  的名命格式2：helloTopicBinding
 * @Author: Timo
 * @Date: 2019/7/25 10:15
 */
@Slf4j
@Configuration
public class TopicRabbitMqConfig {

    /**
     * ##################################################
     * #    queue 的定义
     * #################################################
     */

    @Bean
    Queue fooTopicQueue() {
        return new Queue(RabbitMqConstant.TopicQueueEnum.FOO_TOPIC_QUEUE.name);
    }

    /**
     * foo 插入
     * @return
     */
    @Bean
    Queue fooInsertTopicQueue() {
        return new Queue(RabbitMqConstant.TopicQueueEnum.FOO_INSERT_TOPIC_QUEUE.name);
    }

    /**
     * foo 更新
     * @return
     */
    @Bean
    Queue fooUpdateTopicQueue() {
        return new Queue(RabbitMqConstant.TopicQueueEnum.FOO_UPDATE_TOPIC_QUEUE.name);
    }

    /**
     * foo 删除
     * @return
     */
    @Bean
    Queue fooDeleteTopicQueue() {
        return new Queue(RabbitMqConstant.TopicQueueEnum.FOO_DELETE_TOPIC_QUEUE.name);
    }


    /**
     * ##################################################
     * #    Exchange 的定义
     * #################################################
     */

    @Bean
    TopicExchange fooTopicExchange() {
        return new TopicExchange(RabbitMqConstant.TopicExchangeEnum.FOO_TOPIC_EXCHANGE.name);
    }

    /**
     * ##################################################
     * #    Binding 将队列绑定要交换器上
     * #################################################
     */

    @Bean
    Binding fooTopicBinding() {
        return BindingBuilder.bind(fooTopicQueue()).to(fooTopicExchange()).with("foo.#");
    }


    /**
     * foo插入消息队列绑定
     * @return
     */
    @Bean
    Binding fooInsertQueueBindingFooTopicExchange() {
        return BindingBuilder.bind(fooInsertTopicQueue()).to(fooTopicExchange()).with("foo.insert.topic.queue");
    }


    /**
     * foo 更新消息队列绑定
     * @return
     */
    @Bean
    Binding fooUpdateQueueBindingFooTopicExchange() {
        return BindingBuilder.bind(fooUpdateTopicQueue()).to(fooTopicExchange()).with("foo.update.topic.queue");
    }

    @Bean
    Binding fooDeleteQueueBindingFooTopicExchange() {
        return BindingBuilder.bind(fooDeleteTopicQueue()).to(fooTopicExchange()).with("foo.delete.topic.queue");
    }



}
